package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseSupport;
import com.mt.roll.core.base.IErrorCode;
import com.mt.roll.core.dal.entity.*;
import com.mt.roll.core.dal.service.*;
import com.mt.roll.core.errorcode.ApiErrorCode;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.CodeGeneratorHelper;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.constants.CodeConstant;
import com.mt.roll.wms.params.EquipmentRecipeParam;
import com.mt.roll.wms.params.EquipmentRecipeQueryParam;
import com.mt.roll.wms.params.RecipeActiveSend;
import com.mt.roll.wms.params.RecipeListParam;
import com.mt.roll.wms.service.EquipmentRecipeService;
import com.mt.roll.wms.vo.EquipmentRecipeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2020/12/29 19:55
 */
@Service
@Transactional
public class EquipmentRecipeServiceImpl extends BaseSupport implements EquipmentRecipeService {
    @Autowired
    private EquipmentRecipeServiceBiz equipmentRecipeServiceBiz;

    @Autowired
    private EquipmentRecipeParameterServiceBiz equipmentRecipeParameterServiceBiz;

    @Autowired
    private WorkOrderServiceBiz workOrderServiceBiz;
/*
    @Autowired
    private OrderServiceBiz orderServiceBiz;*/

    @Autowired
    private ProcessFlowEquipmentServiceBiz processFlowEquipmentServiceBiz;

    @Autowired
    private EquipmentServiceBiz equipmentServiceBiz;


    @Override
    public R<List<EquipmentRecipe>> findAllByEquipmentTypeAndRecipeName(EquipmentRecipeQueryParam equipmentRecipeQueryParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, equipmentRecipeQueryParam);
        QueryWrapper<EquipmentRecipe> wrapper = new QueryWrapper<>();
        if(equipmentRecipeQueryParam.getEquipmentId()!=null){
            wrapper.eq(EquipmentRecipe.EQUIPMENT_ID, equipmentRecipeQueryParam.getEquipmentId());
        }
        if(equipmentRecipeQueryParam.getEquipmentRecipeName()!=null){

            wrapper.and(wrapperInner -> wrapperInner
                    .like(EquipmentRecipe.NAME,equipmentRecipeQueryParam.getEquipmentRecipeName())
                    .or()
                    .like(EquipmentRecipe.CODE,equipmentRecipeQueryParam.getEquipmentRecipeName()));
        }
        wrapper.orderByDesc(EquipmentRecipe.CREATE_TIME);
        List<EquipmentRecipe> list = equipmentRecipeServiceBiz.list(wrapper);
        return successful(list);
    }

    @Override
    public R<Boolean> create(EquipmentRecipeParam equipmentRecipeParam) {
        EquipmentRecipe equipmentRecipe = new EquipmentRecipe();
        BeanUtils.copyProperties(equipmentRecipeParam,equipmentRecipe);

        EquipmentRecipe equipmentRecipe1 = setCommonField(equipmentRecipe);
        boolean save = equipmentRecipeServiceBiz.save(equipmentRecipe1);
        if(save){
            return successful(save);
        }else {
            return failed(IErrorCode.CODE_FAILED,"创建失败");
        }
    }

    @Override
    public R<Boolean> update(EquipmentRecipeParam equipmentRecipeParam) {
        EquipmentRecipe equipmentRecipe = new EquipmentRecipe();
        BeanUtils.copyProperties(equipmentRecipeParam,equipmentRecipe);
        EquipmentRecipe equipmentRecipe1 = setCommonField(equipmentRecipe);
        if(equipmentRecipeParam.getEnabled() != null){
            equipmentRecipe1.setEnabled(equipmentRecipeParam.getEnabled());
        }
        boolean res = equipmentRecipeServiceBiz.updateById(equipmentRecipe1);
        if(res){
            return successful(res);
        }else {
            return failed(IErrorCode.CODE_FAILED,"更新失败");
        }
    }

    @Override
    public R<Boolean> delete(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, idParam.getId());
        boolean res = equipmentRecipeServiceBiz.removeById(idParam.getId());
        //把这个配方下 对应的 所有参数也删除。
        QueryWrapper<EquipmentRecipeParameter> wrapper = new QueryWrapper<>();
        wrapper.eq(EquipmentRecipeParameter.EQUIPMENT_RECIPE_ID, idParam.getId());
        boolean remove = equipmentRecipeParameterServiceBiz.remove(wrapper);

        if(res){
            return successful(res);
        }else {
            return failed(IErrorCode.CODE_FAILED,"删除失败");
        }
    }

    @Override
    public R<EquipmentRecipeVo> findById(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, idParam.getId());
        EquipmentRecipe byId = equipmentRecipeServiceBiz.getById(idParam.getId());
        if(byId!=null){
            EquipmentRecipeVo equipmentRecipeVo = EquipmentRecipeVo.builder().build();
            BeanUtils.copyProperties(byId,equipmentRecipeVo);
            return successful(equipmentRecipeVo);
        }
        return failed(IErrorCode.CODE_FAILED,"查找的内容不存在");
    }

/*    *//**
     * 通过workOrderId和equipmentId获取设备配方name
     * @param workOrderId
     * @param equipmentId
     * @return
     *//*
    @Override
    public R<String> getequipmentRecipeForWorkOrder(Long workOrderId, Long equipmentId) {
        //通过工单id查询订单ID,得到订单绑定的工艺
        //通过工艺id和设备id得到配方id
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, workOrderId);
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, equipmentId);
        Long orderId = workOrderServiceBiz.getById(workOrderId).getOrderId();
        Long processFlowId = orderServiceBiz.getById(orderId).getProcessFlowId();
        Long equipmentRecipeId = processFlowEquipmentServiceBiz.getOne(new QueryWrapper<ProcessFlowEquipment>()
                .eq(ProcessFlowEquipment.EQUIPMENT_ID, equipmentId)
                .eq(ProcessFlowEquipment.PROCESS_FLOW_ID, processFlowId)
                .eq(ProcessFlowEquipment.VALID, 1)).getEquipmentRecipeId();
        String recipeName = equipmentRecipeServiceBiz.getById(equipmentRecipeId).getName();
        return successful(recipeName);
    }*/

    @Override
    public R<Boolean> updateRecipeListByToolName(RecipeListParam recipeListParam) {
        //1.获取设备id
        String toolName = recipeListParam.getToolName();
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.eq(Equipment.NAME, toolName);
        //Equipment oneEquipment = equipmentServiceBiz.getOne(wrapper);
        List<Equipment> list = equipmentServiceBiz.list(wrapper);
        if(list.size()<=0){
            return R.failed("设备不存在");
        }
        Equipment oneEquipment = list.get(0);

        //2.删除这个设备下 的所有配方
        QueryWrapper<EquipmentRecipe> wrapperRecipe = new QueryWrapper<>();
        wrapperRecipe.eq(EquipmentRecipe.EQUIPMENT_ID, oneEquipment.getId());
        boolean remove = equipmentRecipeServiceBiz.remove(wrapperRecipe);

        //3.新增配方
        List<String> recipeList = recipeListParam.getRecipeList();
        if(remove){
            for(String recipeName:recipeList){
                EquipmentRecipeParam equipmentRecipe = new EquipmentRecipeParam();
                equipmentRecipe.setEquipmentId(oneEquipment.getId());
                equipmentRecipe.setActivationState(CodeConstant.RECIPE_ACTIVATION_STATE);
                equipmentRecipe.setName(recipeName);
                equipmentRecipe.setCode(CodeGeneratorHelper.getEquipmentRecipeCode());
                R<Boolean> update = create(equipmentRecipe);
                Boolean data = update.getData();
                if(!data){
                    return R.failed();
                }
            }
        }else {
            return R.failed();
        }
        return R.ok(true);
    }

    @Override
    public R<Boolean> simulationForActiveRecipe(RecipeActiveSend recipeActiveSend) {
        String recipeName = recipeActiveSend.getRecipeName();
        String equipmentName = recipeActiveSend.getEquipmentName();
        Integer activationState = recipeActiveSend.getActivationState();

        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.eq(Equipment.NAME, equipmentName);
        //Equipment oneEquipment = equipmentServiceBiz.getOne(wrapper);
        List<Equipment> list = equipmentServiceBiz.list(wrapper);
        if(list.size()<=0){
            return R.failed("设备不存在");
        }
        Equipment oneEquipment = list.get(0);
        QueryWrapper<EquipmentRecipe> recipeQueryWrapper = new QueryWrapper<>();
        recipeQueryWrapper.eq(EquipmentRecipe.NAME, recipeName).eq(EquipmentRecipe.EQUIPMENT_ID,oneEquipment.getId());
        //EquipmentRecipe oneRecipe = equipmentRecipeServiceBiz.getOne(recipeQueryWrapper);
        List<EquipmentRecipe> recipeList = equipmentRecipeServiceBiz.list(recipeQueryWrapper);
        if(recipeList.size() <= 0){
            return R.failed("配方不存在");
        }
        EquipmentRecipe oneRecipe = recipeList.get(0);

        EquipmentRecipeParam equipmentRecipeParam = new EquipmentRecipeParam();
        equipmentRecipeParam.setId(oneRecipe.getId());
        equipmentRecipeParam.setActivationState(activationState);
        R<Boolean> update = update(equipmentRecipeParam);
        return update;
    }
}
